Skip to main content

Overview

This standalone script fetches all NSE stocks with F&O (Futures & Options) availability from the ScanX API. It’s not part of the main pipeline because F&O data is relatively static and doesn’t need daily updates.

Why Standalone?

  • Infrequent Updates: F&O stock list changes rarely (only when new stocks are added/removed from F&O segment)
  • One-Time Fetch: Typically run once to get the initial list, then occasionally to update
  • Independent Data: Doesn’t integrate with other pipeline outputs
  • Manual Trigger: Should be run manually when you need to refresh the F&O stock universe

What It Fetches

The script retrieves comprehensive fundamental and technical data for all F&O-enabled stocks:

Fundamental Fields

  • Market Cap, P/E Ratio, Dividend Yield
  • Revenue, Revenue Growth (1 Year)
  • Net Profit Margin, EBITDA Margin
  • EPS, ROE, ROCE
  • Price-to-Book (P/B) Ratio

Technical Fields

  • OHLC (Open, High, Low, Last Traded Price)
  • Moving Averages (SMA 50, SMA 200)
  • RSI (14-day)
  • Price changes (1 week, 1 month, 1 year, 3 year, 5 year)
  • 52-week highs/lows and distance from them
  • Bollinger Bands, ATR

Output Files

fno_stocks_response.json
JSON file
Contains array of F&O stock objects with all fetched fields. Saved in the current working directory.
Example Output Structure:
[
  {
    "Isin": "INE002A01018",
    "DispSym": "RELIANCE",
    "Mcap": 1750000,
    "Pe": 28.5,
    "Ltp": 2650.30,
    "Roe": 12.5,
    "ROCE": 14.2,
    ...
  }
]

API Reference

fetch_fno_flag_data()

Fetches all F&O stocks from the ScanX API.
None
void
This function takes no parameters.
Returns: None (outputs to file) API Endpoint:
url = "https://ow-scanx-analytics.dhan.co/customscan/fetchdt"
Payload Configuration:
payload = {
    "data": {
        "sort": "Mcap",           # Sort by Market Cap
        "sorder": "desc",         # Descending order
        "count": 500,             # Fetch up to 500 stocks
        "fields": [...],          # 48 fundamental + technical fields
        "params": [
            {"field": "FnoFlag", "op": "", "val": "1"},  # F&O enabled
            {"field": "OgInst", "op": "", "val": "ES"}   # Equity segment
        ],
        "pgno": 0
    }
}
Error Handling:
  • Catches and prints any request exceptions
  • Validates response structure before saving
  • Uses raise_for_status() to catch HTTP errors

When to Run Manually

Run once when setting up the pipeline to get the complete F&O stock universe.
Check after NSE announces new F&O stock additions (typically quarterly).
If you notice a known F&O stock missing from your data.

Usage

python3 fetch_fno_data.py
Expected Output:
Fetching F&O data (FnoFlag: 1) from https://ow-scanx-analytics.dhan.co/customscan/fetchdt...
Successfully fetched 207 F&O stocks. Saved to fno_stocks_response.json

Source Code

import requests
import json
import time

def fetch_fno_flag_data():
    url = "https://ow-scanx-analytics.dhan.co/customscan/fetchdt"
    
    # Payload as specified by the user with FnoFlag: 1
    # Using count: 500 to try and get all 207 in one go
    payload = {
        "data": {
            "sort": "Mcap",
            "sorder": "desc",
            "count": 500,
            "fields": [
                "Isin", "DispSym", "Mcap", "Pe", "DivYeild", "Revenue", 
                "Year1RevenueGrowth", "NetProfitMargin",
                "YoYLastQtrlyProfitGrowth", "EBIDTAMargin", "volume", 
                "PricePerchng1year", "PricePerchng3year",
                "PricePerchng5year", "Ind_Pe", "Pb", "DivYeild", "Eps", 
                "DaySMA50CurrentCandle", "DaySMA200CurrentCandle",
                "DayRSI14CurrentCandle", "ROCE", "Ltp", "Roe", 
                "RtAwayFrom5YearHigh", "RtAwayFrom1MonthHigh",
                "High5yr", "High3Yr", "High1Yr", "High1Wk", "Sym", 
                "PricePerchng1mon", "PricePerchng1week",
                "PricePerchng3mon", "YearlyEarningPerShare", 
                "OCFGrowthOnYr", "Year1CAGREPSGrowth", "NetChangeInCash",
                "FreeCashFlow", "PricePerchng2week", 
                "DayBbUpper_Sub_BbLower", "DayATR14CurrentCandleMul_2",
                "Min5HighCurrentCandle", "Min15HighCurrentCandle", 
                "Min5EMA50CurrentCandle", "Min15EMA50CurrentCandle",
                "Min15SMA100CurrentCandle", "Open", "BcClose", 
                "Rmp", "PledgeBenefit"
            ],
            "params": [
                {"field": "FnoFlag", "op": "", "val": "1"},
                {"field": "OgInst", "op": "", "val": "ES"}
            ],
            "pgno": 0,
            "sorder": "desc",
            "sort": "Mcap"
        }
    }

    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
        "Accept": "application/json, text/plain, */*",
        "Origin": "https://scanx.dhan.co",
        "Referer": "https://scanx.dhan.co/"
    }

    print(f"Fetching F&O data (FnoFlag: 1) from {url}...")
    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        
        data = response.json()
        
        if 'data' in data and isinstance(data['data'], list):
            cleaned_stocks = data['data']
            # Save the cleaned list to a JSON file
            output_file = "fno_stocks_response.json"
            with open(output_file, "w") as f:
                json.dump(cleaned_stocks, f, indent=4)
            print(f"Successfully fetched {len(cleaned_stocks)} F&O stocks. Saved to {output_file}")
        else:
            print("Response structure might be different than expected.")
            
    except Exception as e:
        print(f"Error fetching data: {e}")

if __name__ == "__main__":
    fetch_fno_flag_data()

Dependencies

  • requests: HTTP library for API calls
  • json: JSON serialization/deserialization
  • time: (imported but not used in current version)
This script uses hardcoded headers and does not import from pipeline_utils.py. If you need user-agent rotation, consider refactoring to use get_headers().